home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / modules / oauth.js < prev    next >
Text File  |  2008-08-08  |  5KB  |  157 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2008.
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Anant Narayanan <anant@kix.in>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. const EXPORTED_SYMBOLS = ['OAuth'];
  38.  
  39. const Cc = Components.classes;
  40. const Ci = Components.interfaces;
  41. const Cr = Components.results;
  42. const Cu = Components.utils;
  43.  
  44. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  45. Cu.import("resource://weave/log4moz.js");
  46. Cu.import("resource://weave/constants.js");
  47. Cu.import("resource://weave/util.js");
  48. Cu.import("resource://weave/async.js");
  49. Cu.import("resource://weave/identity.js");
  50. Cu.import("resource://weave/engines.js");
  51.  
  52. Function.prototype.async = Async.sugar;
  53.  
  54. Utils.lazy(this, 'OAuth', OAuthSvc);
  55.  
  56. function OAuthSvc() {
  57.   this._init();
  58. }
  59. OAuthSvc.prototype = {
  60.   _logName: "OAuth",
  61.   _keyring: null,
  62.   _consKey: null,
  63.   _bulkID: null,
  64.   _rsaKey: null,
  65.   _token: null,
  66.   _cback: null,
  67.   _uid: null,
  68.   _pwd: null,
  69.   _pas: null,
  70.   _cb1: null,
  71.   _cb2: null,
  72.   
  73.   _init: function OAuth__init() {
  74.     this._log = Log4Moz.Service.getLogger("Service." + this._logName);
  75.     this._log.level = "Debug";
  76.     this._log.info("OAuth Module Initialized");
  77.   },
  78.  
  79.   setToken: function OAuth_setToken(token, cback) {
  80.     this._token = token;
  81.     this._cback = cback;
  82.   },
  83.   
  84.   setUser: function OAuth_setUser(username, password, passphrase) {
  85.     this._uid = username;
  86.     this._pwd = password;
  87.     this._pas = passphrase;
  88.   },
  89.   
  90.   validate: function OAuth_getName(obj, cb) {
  91.     if (!this._token || !this._uid || !this._pwd)
  92.       cb(obj, false);
  93.  
  94.     var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
  95.               createInstance(Ci.nsIXMLHttpRequest);
  96.     var key = btoa(this._uid + ":" + this._pwd);
  97.     
  98.     req.onreadystatechange = function(e) {
  99.       if (req.readyState == 4) {
  100.         if (req.status == 200) {
  101.           var fields = req.responseText.split(',');
  102.           cb(obj, fields[0], fields[1], fields[2]);
  103.         } else {
  104.           cb(obj, req.responseText);
  105.         }
  106.       }
  107.     };
  108.     req.open('GET', 'https://services.mozilla.com/api/oauth/info.php?token=' + this._token + '&key=' + key);
  109.     req.send(null);
  110.   },
  111.   
  112.   finalize: function OAuth_finalize(cb1, cb2, bundle) {
  113.     this._cb1 = cb1;
  114.     this._cb2 = cb2;
  115.     this._bundle = bundle;
  116.  
  117.     var bmkEngine = Engines.get('bookmarks');
  118.     var bmkRstore = bmkEngine._remote;
  119.  
  120.     this._keyring = bmkRstore.keys;
  121.     this._keyring.getKeyAndIV(Utils.bind2(this, this._gotBulkKey), ID.get('WeaveID'));
  122.   },
  123.   
  124.   _gotBulkKey: function OAuth_gotBulkKey() {
  125.     let consID = new Identity();
  126.     consID.pubkey = this._rsaKey;
  127.     consID.username = this._consKey;
  128.     this._cb1(this._bundle);
  129.     this._log.info("Updating keyring for 3rd party access");
  130.     this._keyring.setKey(Utils.bind2(this, this._done), ID.get('WeaveID'), consID);
  131.   },
  132.   
  133.   _done: function OAuth__done() {
  134.     var cb = this._cb2;
  135.     var bu = this._bundle;
  136.  
  137.     if (!this._token || !this._uid || !this._pwd)
  138.       cb(this._bundle, false);
  139.     
  140.     var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
  141.               createInstance(Ci.nsIXMLHttpRequest);
  142.     var key = btoa(this._uid + ":" + this._pwd);
  143.     
  144.     req.onreadystatechange = function(e) {
  145.       if (req.readyState == 4) {
  146.         if (req.status == 200 && req.responseText == "1") {
  147.           cb(bu, true);
  148.         } else {
  149.           cb(bu, false);
  150.         }
  151.       }
  152.     };
  153.     req.open('GET', 'https://services.mozilla.com/api/oauth/update.php?token=' + this._token + '&key=' + key);
  154.     req.send(null);
  155.   }
  156. };
  157.